package com.yisin.pbox.frame.util;

import java.util.Map;

import com.yisin.pbox.frame.common.reflect.YXObject;

public class SqlUtil {

	public static void main(String[] args) {
	}

	public static String name2table(String className) {
		char chr = '0';
		String result = className;
		boolean has = true;
		while (has) {
			has = false;
			for (int i = 0; i < result.length(); i++) {
				chr = result.charAt(i);
				if (chr >= 'A' && chr <= 'Z') {
					result = result.substring(0, i) + ((i > 0 ? "_" : "") + chr).toLowerCase() + result.substring(i + 1);
					has = true;
					break;
				}
			}
		}
		return result;
	}

	public static String createInsertSql(Object obj, String key) {
		Map<String, Object> map = YXObject.getValue(obj);
		String className = obj.getClass().getSimpleName();
		String tableName = name2table(className);
		StringBuffer sb = new StringBuffer("insert into ");
		StringBuffer feild = new StringBuffer("");
		StringBuffer pred = new StringBuffer("");
		sb.append(tableName).append("(");
		for (Map.Entry<String, Object> ent : map.entrySet()) {
			feild.append(ent.getKey()).append(",");
			pred.append("?,");
		}
		sb.append(feild.toString().substring(0, feild.length() - 1));
		sb.append(") values (");
		sb.append(pred.toString().substring(0, pred.length() - 1));
		sb.append(")");
		return sb.toString();
	}
}
